
********************************************************************************	
//Final table
cd $pathdata_processed
use terminales, clear

merge 1:m npid using chance, keep(1 3) nogen 

//Sample selection 
keep if (inlist(nus2000_vg3, 401101, 441106) & year_vg3 <= 2007) | (inlist(nus2000_vg3, 401111, 401112) & year_vg3 >= 2008) //general or economic and business program
keep if status_vg3 == 2 & heldel_vg3 == 1 //student status & full time
drop if N_tilg>1 //enrolled in one course
drop if nb_komp>1
drop if missing(skole_foretak_vg3) | missing(skolekom_vg3) //information on school
drop if inlist(fylke_vg3, 21, 25)
keep if year_vg3<=2009					  
drop if missing(yob) | missing(female)
drop if missing(videregaendepoeng3) //high school gpa

drop if missing(nb_kurs)
drop if nb_exam == 0 
drop if missing(exam_grades)
drop if nb_course_1 > 0 & !missing(nb_course_1)

//Winsorizing (to avoid outliers)
foreach var in mwyrkinnt_c fwyrkinnt_c wyrkinnt_c1 wyrkinnt_c10 {
	replace `var' = 0 if `var'<0
	winsor2 `var', replace cuts(0 99)
}

//Missing background variables
foreach var in ms_gpa mwyrkinnt_c fwyrkinnt_c {
	gen m_`var' = missing(`var')
	bysort year_vg3: egen mean = mean(`var')
	replace `var' = mean if missing(`var')
	drop mean
}
foreach var in nmpid nfpid {
		gen m_`var' = missing(`var')
} 
foreach var in mbu_kl fbu_kl {
	gen m_`var' = missing(`var')
	bysort year_vg3: egen mode = mode(`var')
	replace `var' = mode if missing(`var')
	drop mode
} 
foreach var in myob fyob {
	gen m_`var' = missing(`var')
	bysort year_vg3: egen mode = mode(`var')
	replace `var' = mode if missing(`var')
	drop mode
}  

//Controls
gen age = year_vg3+1-yob
gen age2 = age*age

gen p_educ = (mbu_kl+fbu_kl)/2
gen p_educ2 = p_educ*p_educ

gen p_inc = (fwyrkinnt_c+mwyrkinnt_c)/2
gen ln_p_inc = ln(p_inc+1)

foreach j in m f {
	gen `j'age = year_vg3 - `j'yob
}

gen p_age = (mage+fage)/2
gen p_age2 = p_age*p_age
	
gen m_mage = m_myob
gen m_fage = m_fyob
	
rename videregaendepoeng3 vide3
foreach var in teachers_grades ms_gpa vide3 admission_score_vg3 admission_score exam_grades oral_grades {
	bysort year_vg3: egen std`var' = std(`var')
	g std`var'2 = std`var'* std`var'
}
gen m_stdms_gpa = m_ms_gpa

//Cluster
egen group = group(year_vg3 skole_foretak_vg3)

//Jobs
foreach var in wyrkinnt_c1 wyrkinnt_c10 {
   g ln_`var' =  ln(`var')
}

//Educ
gen bu_sup = bu_kl - 13
replace bu_sup = 0 if bu_sup<0

keeporder npid ///
	yob mob age age2 female nmpid nfpid m_nfpid m_nmpid ///
	mbu_kl m_mbu_kl ///
	fbu_kl m_fbu_kl ///
	p_educ p_educ2 ///
	mwyrkinnt_c fwyrkinnt_c p_inc ln_p_inc m_mwyrkinnt_c m_fwyrkinnt_c ///
	myob mage m_mage m_myob fyob fage m_fage m_fyob p_age p_age2 ///
	year_vg3 fylke_vg3 public_vg3 skole_foretak_vg3 group heldel_vg3 status_vg3 nus2000_vg3 nb_kurs nb_exam nb_oral expected_gpa predicted_gpa variance_gpa chance chance_sd *exam_grades* *teachers_grades* sd_teachers_grades mean_skr_hat sd_skr_hat oral_grades matieres* Tirage_final Fagkode ///
	vide*1 vide*2 *vide*3* admission_score_vg3 stdadmission_score_vg3 stdadmission_score_vg32 admission_score admission_score stdadmission_score stdadmission_score2 ///
	ms_gpa m_ms_gpa stdms_gpa stdms_gpa2 m_stdms_gpa ///
	completion hs_gs_c he_e first_year_e he_c bu_kl bu_sup year_c last_year_e ///
	rank_f_he nb_available_prog share_available_prog ///
	general_intro_course_f_he humanities_f_he social_science_f_he teaching_f_he health_f_he science_f_he enigneering_f_he technology_f_he business_f_he law_f_he medicine_f_he other_f_he ///
	first_job still_educ no_first_job year_1st_job ///
	wyrkinnt1 wyrkinnt_c1 ln_wyrkinnt_c1 ///		
	job10 ///
	wyrkinnt10 wyrkinnt_c10 ln_wyrkinnt_c10 
	
save terminales_fin, replace
